package com.feri.fitness.network.oss.service.impl;

import com.feri.fitness.common.vo.R;
import com.feri.fitness.network.censor.core.BaiduCensorUtil;
import com.feri.fitness.network.oss.core.AliOssUtil;
import com.feri.fitness.network.oss.dao.OsslogDao;
import com.feri.fitness.network.oss.entity.Osslog;
import com.feri.fitness.network.oss.service.intf.OsslogService;
import com.feri.fitness.network.util.FileUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 千锋郑州Java学科欢迎你
 * @program: Fitness
 * @description: 业务接口实现层
 * @author: Feri
 * @create: 2023-01-12 08:59:28
 */
@Service
public class OsslogServiceImpl implements OsslogService {
    /**
    *注入持久层对象*/
    @Resource
    private OsslogDao dao;

    @Override
    public R uploadImg(MultipartFile file) {
        String url=upload(file,1);
        if(StringUtils.hasLength(url)){
            return R.ok(url);
        }else {
            return R.fail();
        }
    }

    @Override
    public R uploadVideo(MultipartFile file) {
        String url=upload(file,2);
        if(StringUtils.hasLength(url)){
            return R.ok(url);
        }else {
            return R.fail();
        }
    }

    private String upload(MultipartFile file,int type){
        //1.验证上传的内容是否存在
        if(!file.isEmpty()){
            //2.上传名称进行处理
            String name= FileUtil.rename(file.getOriginalFilename());
            try {
                //3.执行上传-阿里云Oss，Oss内部 有图片鉴别
                String url = AliOssUtil.upload(file.getBytes(), name);
                //4.记录数据库
                if(StringUtils.hasLength(url)){
                    dao.insert(new Osslog(AliOssUtil.bucket,name,url,type));
                }
                return url;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
    * 查询 所有数据*/
    @Override
    public R queryAll() {
        return R.ok(dao.selectList(null));
    }
}